package com.cleaningcloud.erp.service.impl;

import com.cleaningcloud.common.core.utils.DateUtils;
import com.cleaningcloud.common.core.utils.UniqueIdUtils;
import com.cleaningcloud.common.security.utils.CommunityTypeConversionUtils;
import com.cleaningcloud.common.security.utils.SecurityUtils;
import com.cleaningcloud.erp.api.domain.ErpCommodityStockAlter;
import com.cleaningcloud.erp.api.domain.ErpWareCommodityNumber;
import com.cleaningcloud.erp.api.utils.StockCommodityAlterUtils;
import com.cleaningcloud.erp.domain.ErpWareReporting;
import com.cleaningcloud.erp.domain.dto.ErpWareReportingDto;
import com.cleaningcloud.erp.domain.vo.ErpWareReportingDetailsVo;
import com.cleaningcloud.erp.domain.vo.ErpWareReportingVo;
import com.cleaningcloud.erp.mapper.ErpCommodityStockAlterMapper;
import com.cleaningcloud.erp.mapper.ErpWareCommodityNumberMapper;
import com.cleaningcloud.erp.mapper.ErpWareReportingMapper;
import com.cleaningcloud.erp.service.IErpWareReportingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * 报损单Service业务层处理
 *
 * @author cleaningcloud
 * @date 2024-10-08
 */
@Service
public class ErpWareReportingServiceImpl implements IErpWareReportingService {
    @Autowired
    private ErpWareReportingMapper erpWareReportingMapper;

    @Autowired
    private ErpWareCommodityNumberMapper erpWareCommodityNumberMapper;

    @Autowired
    private ErpCommodityStockAlterMapper erpCommodityStockAlterMapper;

    /**
     * 查询报损单
     *
     * @param wareReportingId 报损单主键
     * @return 报损单
     */
    @Override
    public ErpWareReportingVo selectErpWareReportingByWareReportingId(Long wareReportingId) {
        ErpWareReportingVo erpWareReportingVo = erpWareReportingMapper.selectErpWareReportingByWareReportingId(wareReportingId);
        for (ErpWareReportingDetailsVo erpWareReportingDetailsVo : erpWareReportingVo.getDetailsList()) {
            erpWareReportingDetailsVo.setCommodityType(CommunityTypeConversionUtils.conversion(erpWareReportingDetailsVo.getCommodityType()));
        }
        return erpWareReportingVo;
    }

    /**
     * 查询报损单列表
     *
     * @param erpWareReporting 报损单
     * @return 报损单
     */
    @Override
    public List<ErpWareReportingVo> selectErpWareReportingList(ErpWareReporting erpWareReporting) {
        return erpWareReportingMapper.selectErpWareReportingList(erpWareReporting);
    }

    /**
     * 新增报损单
     *
     * @param erpWareReporting 报损单
     * @return 结果
     */
    @Override
    @Transactional
    public int insertErpWareReporting(ErpWareReportingDto erpWareReporting) {
        erpWareReporting.setCreateTime(DateUtils.getNowDate());
        erpWareReporting.setCreateId(SecurityUtils.getUserId());
        erpWareReporting.setCreateName(SecurityUtils.getUsername());
        erpWareReporting.setApproveStatus(erpWareReporting.getApproveStatus() == null ? 0 : erpWareReporting.getApproveStatus());
        erpWareReporting.setFlag("Y");
        erpWareReporting.setWareReportingOdd("BS" + UniqueIdUtils.generateUniqueId());
        erpWareReportingMapper.insertErpWareReporting(erpWareReporting);
        if (erpWareReporting.getDetailsList() != null && !erpWareReporting.getDetailsList().isEmpty()) {
            erpWareReporting.getDetailsList().forEach(item -> {
                item.setWareReportingId(erpWareReporting.getWareReportingId());
            });
            erpWareReportingMapper.batchInsertWareReportingDetails(erpWareReporting.getDetailsList());
        }
        return 1;
    }

    /**
     * 修改报损单
     *
     * @param erpWareReporting 报损单
     * @return 结果
     */
    @Override
    @Transactional
    public int updateErpWareReporting(ErpWareReportingDto erpWareReporting) {
        erpWareReporting.setApproveStatus(0);
        erpWareReportingMapper.updateErpWareReporting(erpWareReporting);
        // 先删除库存盘点明细
        erpWareReportingMapper.deleteWareReportingDetailsByWareCheckId(erpWareReporting.getWareReportingId());
        if (erpWareReporting.getDetailsList() != null && !erpWareReporting.getDetailsList().isEmpty()) {
            erpWareReporting.getDetailsList().forEach(item -> {
                item.setWareReportingId(erpWareReporting.getWareReportingId());
            });
            erpWareReportingMapper.batchInsertWareReportingDetails(erpWareReporting.getDetailsList());
        }
        return 1;
    }

    /**
     * 批量删除报损单
     *
     * @param wareReportingIds 需要删除的报损单主键
     * @return 结果
     */
    @Override
    public int deleteErpWareReportingByWareReportingIds(Long[] wareReportingIds) {
        return erpWareReportingMapper.deleteErpWareReportingByWareReportingIds(wareReportingIds);
    }

    /**
     * 删除报损单信息
     *
     * @param wareReportingId 报损单主键
     * @return 结果
     */
    @Override
    public int deleteErpWareReportingByWareReportingId(Long wareReportingId) {
        return erpWareReportingMapper.deleteErpWareReportingByWareReportingId(wareReportingId);
    }

    @Override
    @Transactional
    public int auditErpWareReporting(ErpWareReporting erpWareReporting) {
        ErpWareReportingVo wareReportingVo = erpWareReportingMapper.selectErpWareReportingByWareReportingId(erpWareReporting.getWareReportingId());
        if (wareReportingVo == null) {
            throw new RuntimeException("报损单查询失败");
        }
        wareReportingVo.setApproveId(SecurityUtils.getUserId());
        wareReportingVo.setApproveStatus(erpWareReporting.getApproveStatus());
        wareReportingVo.setApproveName(SecurityUtils.getUsername());
        wareReportingVo.setApproveTime(DateUtils.getNowDate());
        if (wareReportingVo.getApproveStatus() == 2) {
            wareReportingVo.setRejectTime(DateUtils.getNowDate());
            wareReportingVo.setRejectReason(erpWareReporting.getRejectReason());
        }
        // 审核通过 根据盘点数量修改商品库存
        if (wareReportingVo.getApproveStatus() == 1) {
            // 商品库存信息
            ErpWareCommodityNumber query = new ErpWareCommodityNumber();
            if (wareReportingVo.getDetailsList() != null && !wareReportingVo.getDetailsList().isEmpty()) {
                wareReportingVo.getDetailsList().forEach(item -> {
                    // 根据商品id和调出仓库id查询对应库存数据
                    query.setWareId(wareReportingVo.getWareId());
                    query.setCommodityId(item.getCommodityId());
                    ErpWareCommodityNumber erpWareCommodityNumber = erpWareCommodityNumberMapper.selectByCommodityIdAndWareId(query);
                    // 根据报损数量进行修改 库存数量 - 报损数量
                    if (erpWareCommodityNumber.getCommodityNumber() < item.getCommodityNumber()) {
                        throw new RuntimeException("报损数量不能大于库存数量");
                    }
                    erpWareCommodityNumber.setCommodityNumber(erpWareCommodityNumber.getCommodityNumber() - item.getCommodityNumber());
                    erpWareCommodityNumberMapper.updateErpWareCommodityNumber(erpWareCommodityNumber);

                    // 新增库存商品变更记录信息
                    ErpCommodityStockAlter erpCommodityStockAlter = StockCommodityAlterUtils.AssemblyInfo(item.getCommodityId(), BigDecimal.valueOf(item.getCommodityNumber()).negate(), "5", wareReportingVo.getWareId());
                    erpCommodityStockAlterMapper.insertErpCommodityStockAlter(erpCommodityStockAlter);
                });
            }
        }
        return erpWareReportingMapper.updateErpWareReporting(wareReportingVo);
    }
}
